function onCavas(e, bodyData, loop_data, unit, initial_j, winWidth, callback) {
  var that = this;
  var click_xPos = e.detail.x;
  var initial_j = 0;
  var x_judge = winWidth/7 * initial_j ;
  if (click_xPos <= 50) {
    initial_j = 0;
  } else if (click_xPos > winWidth / 7 && click_xPos <= winWidth / 7 * 2) {
    initial_j = 1;
  } else if (click_xPos > winWidth / 7 * 2 && click_xPos <= winWidth / 7 * 3) {
    initial_j = 2;
  } else if (click_xPos > winWidth / 7 * 3 && click_xPos <= winWidth / 7 * 4) {
    initial_j = 3;
  } else if (click_xPos > winWidth / 7 * 4 && click_xPos <= winWidth / 7 * 5) {
    initial_j = 4;
  } else if (click_xPos > winWidth / 7 * 5 && click_xPos <= winWidth / 7 * 6) {
    initial_j = 5;
  } else if (click_xPos > winWidth / 7 * 6) {
    initial_j = 6;
  }
  if (callback) callback(initial_j)
  switch_canvas(bodyData, loop_data, unit, initial_j, winWidth);
  return switch_canvas(bodyData, loop_data, unit, initial_j, winWidth);
}

function switch_canvas(bodyData, loop_data, unit, initial_j, winWidth) {

  var that = this;
  var ctx = wx.createCanvasContext('canvas_chart');
  ctx.setFontSize(14);
  ctx.drawImage("/images/my/canvas_bg.png", 0, 0, winWidth, 260);
  if (!bodyData || bodyData.length==0){
    bodyData=[];
    for(var i=0;i<7;i++){
      var temp={};
      temp[loop_data]=0;
      bodyData.push(temp);
    }
    initial_j=bodyData.length-1;
  }
  var data_array = [];
  for (var i = 0; i < bodyData.length; i++) {
    data_array.push(bodyData[i][loop_data]);
  }
  var data_max = Math.max.apply(null, data_array);
  if( data_max == 0 ){
      data_max = 195;
  } 
  var bili = 195 / data_max;
  var x_axis, y_axis, width, height;
  var my_gradient = ctx.createLinearGradient(0, 0, 0, 170);
  my_gradient.addColorStop(0, '#FFD015');//255,149,0
  my_gradient.addColorStop(1, '#FFD015');//255,94,58
  ctx.fillStyle = my_gradient;
  for (var j = 0; j < bodyData.length; j++) {
    width = 25;
    height = bili * bodyData[j][loop_data];
    // x_axis = 19 + 52 * j;
    // var offset = (width - 65) / 6;
    x_axis = (winWidth - 65) / 6 * j + 20;
    y_axis = 260 - height;
    //把每一个柱子给画出来、
    ctx.fillRect(x_axis, y_axis, width, height);
  }
  var initial_height = bili * bodyData[initial_j][loop_data]; 
  //首先是半圆形标志,然后是提示的对话框,最后是对话框里的文字
   // 文字字号：22px
  ctx.drawImage("/images/my/bx-Node.png", (winWidth - 65) / 6 * initial_j + 20, 260 - initial_height - 12.5, 25, 25);
  ctx.drawImage("/images/my/btn.png", (winWidth - 65) / 6 * initial_j +1 , 260 - initial_height - 38, 64, 30);
  ctx.fillText(bodyData[initial_j][loop_data]+unit, (winWidth - 65) / 6 * initial_j + 18, 260 - initial_height - 22);
  ctx.stroke();
  ctx.draw();

  return bodyData[initial_j][loop_data];

}

module.exports = { 'onCavas': onCavas, 'switch_canvas': switch_canvas };